package com.zwz.controller;

import com.zwz.dto.AdministrativePenaltyDTO;
import com.zwz.entity.AdministrativePenalty;
import com.zwz.service.AdministrativePenaltyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/penalties")
public class AdministrativePenaltyController {

    @Autowired
    private AdministrativePenaltyService service;

    @PostMapping
    public ResponseEntity<?> create( String enterpriseId) {
        try {
            AdministrativePenaltyDTO dto = service.createWithMain( enterpriseId);
            return ResponseEntity.status(HttpStatus.CREATED).body(dto);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/search")
    public ResponseEntity<List<AdministrativePenalty>> searchByDate(
            @RequestParam @DateTimeFormat(pattern="yyyy-MM-dd") Date start,
            @RequestParam @DateTimeFormat(pattern="yyyy-MM-dd") Date end) {
        return ResponseEntity.ok(service.getByDateRange(start, end));
    }
}